<?php

class Admin_WebController extends CMS_Controller_Action_Admin {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $lang = Common_Translate::getLanguage();
        $content = new CMS_Models_LangWeb();
        $this->view->contents = $content->getWebByLang($lang);
    }

    public function editwebAction() {
        $form = $this->editWebForm();
        $this->view->form = $form;
        $langs = new CMS_Models_Lang();
        $this->view->langs = $langs->getEnabledLangs();
        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $web = new CMS_Models_Web();
                //var_dump($values);exit;
                $web = $web->getWebById($values['id_web']);
                $web->author = $values['common']['author'];
                $web->copyright = $values['common']['copyright'];
                $web->save();
                $langs = new CMS_Models_Lang();
                $langs = $langs->getEnabledLangs();
                foreach ($langs as $l) {
                    $langWeb = new CMS_Models_LangWeb();
                    $langWeb = $langWeb->getWebById($web->id, $l['prefix2']);
                    if (count($langWeb) <= 0) {
                        $langWeb = new Zend_Models_LangWeb();
                        $langWeb = $langWeb->createRow();
                    }
                    $langWeb->id_web = $values['id_web'];
                    $langWeb->lang = $l['prefix2'];
                    $langWeb->dc_title = $values[$l['prefix2']]['dc_title_' . $l['prefix2']];
                    $langWeb->dc_description = $values[$l['prefix2']]['dc_description_' . $l['prefix2']];
                    $langWeb->dc_keywords = $values[$l['prefix2']]['dc_keywords_' . $l['prefix2']];
                    $langWeb->description = $values[$l['prefix2']]['description_' . $l['prefix2']];
                    $langWeb->keywords = $values[$l['prefix2']]['keywords_' . $l['prefix2']];
                    $langWeb->save();
                }
                $this->_redirect("/admin/web/index");
            }
        }
    }

    private function editWebForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/web/editweb")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $lang = Common_Translate::getLanguage();
        $id_web = $this->_request->getParam('id_web');

        $web = new CMS_Models_Web();
        $web = $web->getWebById($id_web);
        /*         * ** Datos comunes *** */
        $common = new Zend_Form_SubForm();
        $author = $common->createElement('text', 'author');
        $author->setAttrib("class", "text-long");
        $author->setLabel('Autor:');
        $author->setValue($web->author);
        $common->addElement($author);
        $copyright = $common->createElement('text', 'copyright');
        $copyright->setAttrib("class", "text-long");
        $copyright->setLabel('Copyright:');
        $copyright->setValue($web->copyright);
        $common->addElement($copyright);
        $form->addSubForms(array("common" => $common));
        /*         * ** Fin datos comunes ** */
        /*         * ** Datos por idioma *** */
        $langs = new CMS_Models_Lang();
        $langs = $langs->getEnabledLangs();
        foreach ($langs as $l) {
            $sub = new Zend_Form_SubForm();
            $langWeb = new CMS_Models_LangWeb();
            $langWeb = $langWeb->getWebByKey($web->id, $l['prefix2']);
            $dc_title = $sub->createElement("textarea", "dc_title_" . $l['prefix2']);
            $dc_title->setAttrib("class", "text-long");
            $dc_title->setLabel($this->view->t->_('Dc Keywords:'));
            $dc_title->setValue($langWeb['dc_title']);
            $sub->addElement($dc_title);

            $dc_description = $sub->createElement("textarea", "dc_description_" . $l['prefix2']);
            $dc_description->setAttrib("class", "text-long");
            $dc_description->setLabel($this->view->t->_('Dc Description:'));
            $dc_description->setValue($langWeb['dc_description']);
            $sub->addElement($dc_description);

            $dc_keywords = $sub->createElement("textarea", "dc_keywords_" . $l['prefix2']);
            $dc_keywords->setAttrib("class", "text-long");
            $dc_keywords->setLabel($this->view->t->_('Dc Keywords:'));
            $dc_keywords->setValue($langWeb['dc_keywords']);
            $sub->addElement($dc_keywords);

            $description = $sub->createElement("textarea", "description_" . $l['prefix2']);
            $description->setAttrib("class", "text-long");
            $description->setLabel($this->view->t->_('Description:'));
            $description->setValue($langWeb['description']);
            $sub->addElement($description);

            $keywords = $sub->createElement("textarea", "keywords_" . $l['prefix2']);
            $keywords->setAttrib("class", "text-long");
            $keywords->setLabel($this->view->t->_('Keywords:'));
            $keywords->setValue($langWeb['keywords']);
            $sub->addElement($keywords);

            $form->addSubForms(array($l['prefix2'] => $sub));
        }
        $id = $form->createElement("hidden", "id_web");
        $id->setValue($web->id);
        $form->addElement($id);
        $form->addElement('submit', 'add', array('label' => 'Aceptar'));
        return $form;
    }

}

